<html>
<head><meta charset="utf-8"><title>WebAssembly · project-inline-asm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/index.html">project-inline-asm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html">WebAssembly</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="183496709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183496709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183496709">(Dec 15 2019 at 17:31)</a>:</h4>
<p>LLVM, and Rust's current <code>asm!</code>, both support inline assembly for WebAssembly. Take a look at <a href="https://godbolt.org/z/h9C4KE" target="_blank" title="https://godbolt.org/z/h9C4KE">https://godbolt.org/z/h9C4KE</a> (previously linked from an internals thread). I think it seems worthwhile to document such support in the initial RFC.</p>



<a name="183496713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183496713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183496713">(Dec 15 2019 at 17:32)</a>:</h4>
<p>I'm currently looking through LLVM's source trying to find more information on the constraints LLVM supports for inline assembly on a WebAssembly target, since the documentation doesn't talk about that.</p>



<a name="183497481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183497481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183497481">(Dec 15 2019 at 17:55)</a>:</h4>
<p>...interesting. Apparently that backend <em>only</em> supports the <code>"r"</code> constraint, treating it as an index for a local; you can't access things directly from the stack in any way. That makes a certain amount of sense, and hopefully the optimizer can eliminate inefficiencies like loading the same local multiple times.</p>



<a name="183497494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183497494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183497494">(Dec 15 2019 at 17:55)</a>:</h4>
<p>So, it seems worth documenting how <code>asm!</code> will work with WebAssembly, as a somewhat unique backend that doesn't have named registers at all.</p>



<a name="183497837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183497837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183497837">(Dec 15 2019 at 18:04)</a>:</h4>
<p>Is there any actual use case for inline asm for wasm though? The only one I can think of is <code>hint::black_box</code>.</p>



<a name="183498431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183498431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183498431">(Dec 15 2019 at 18:21)</a>:</h4>
<p>Sure. Directly writing algorithms in assembly, experimenting with new instructions...</p>



<a name="183566185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183566185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183566185">(Dec 16 2019 at 16:37)</a>:</h4>
<p>A direct line to WASM sqrt on stable would be nice. Currently you have to call the unstable core intrinsic (Nightly) or go through std (and the rest of the crate might generally otherwise be no_std otherwise)</p>



<a name="183569434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183569434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183569434">(Dec 16 2019 at 17:07)</a>:</h4>
<p>This shouldn't stop us from stabilizing intrinsics like that, but yes.</p>



<a name="183570776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183570776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183570776">(Dec 16 2019 at 17:22)</a>:</h4>
<p>Oh there's a whole mess of issues on that particular intrinsic :P</p>



<a name="183581617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183581617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183581617">(Dec 16 2019 at 19:26)</a>:</h4>
<p>Oh? Do tell.</p>



<a name="183598313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183598313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183598313">(Dec 16 2019 at 22:33)</a>:</h4>
<p>(deleted)</p>



<a name="183623244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183623244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183623244">(Dec 17 2019 at 06:58)</a>:</h4>
<p>The optimizer can't eliminate loading the same local multiple times if you do it within an asm block.</p>



<a name="183623256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183623256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183623256">(Dec 17 2019 at 06:59)</a>:</h4>
<p>Unless you want to deviate from the general behavior that the optimizer doesn't inspect asm blocks.</p>



<a name="183623338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183623338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183623338">(Dec 17 2019 at 07:01)</a>:</h4>
<p>I think that ideally we'd have a more efficient way to pass inputs to WebAssembly asm blocks, which would put things directly on the stack for you.  Of course, that would require backend support, and it's far from urgent.</p>



<a name="183639357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183639357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183639357">(Dec 17 2019 at 11:27)</a>:</h4>
<p>I don't mean the rustc optimizer in that case.</p>



<a name="183639410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183639410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183639410">(Dec 17 2019 at 11:28)</a>:</h4>
<p>I mean the wasm optimizer. It should trivially be able to eliminate "store to 1 then load to 1".</p>



<a name="183830731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183830731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183830731">(Dec 19 2019 at 09:55)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> which wasm optimizer do you use ?</p>



<a name="183830809"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183830809" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183830809">(Dec 19 2019 at 09:56)</a>:</h4>
<p>AFAIK the way to optimize wasm is to generate it from, e.g., C code, using an optimizing compiler toolchain to WASM (this won't optimize that because the toolchain won't look at the inline assembly string)</p>



<a name="183830888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183830888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183830888">(Dec 19 2019 at 09:57)</a>:</h4>
<p>but once that is generated, most (all?) of the WASM -&gt; machine code generators will just generate machine code without performing any significant optimizations</p>



<a name="183830918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183830918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183830918">(Dec 19 2019 at 09:57)</a>:</h4>
<p>and AFAIK none of them optimizes across two consecutive wasm instructions</p>



<a name="183831089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183831089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183831089">(Dec 19 2019 at 09:59)</a>:</h4>
<p>Is there a tool that takes WASM and optimizes it into better WASM ?</p>



<a name="183855764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/183855764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#183855764">(Dec 19 2019 at 15:32)</a>:</h4>
<p>There's wasm-opt.</p>



<a name="184101411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184101411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184101411">(Dec 23 2019 at 12:37)</a>:</h4>
<p>Cool! Does it run as part of the LLVM pipeline ?</p>



<a name="184108979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184108979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184108979">(Dec 23 2019 at 14:46)</a>:</h4>
<p>I believe that one is generally expected to run it themselves as part of their <code>make</code> script</p>



<a name="184375404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184375404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184375404">(Dec 28 2019 at 13:34)</a>:</h4>
<p>We would kind of need LLVM to guarantee never to run those as part of its pipeline.</p>



<a name="184375405"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184375405" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184375405">(Dec 28 2019 at 13:34)</a>:</h4>
<p>(If the semantics of <code>asm!("...")</code> are that we never inspect the assembly string, since otherwise running those would "modify" the assembly string)</p>



<a name="184375423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184375423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184375423">(Dec 28 2019 at 13:35)</a>:</h4>
<p>That is, an argument in favor of not specifying that the assembly string is preserved "verbatim" (modulo interpolation), is that it prevents optimizations on the resulting machine code, e.g., via <code>wasm-opt</code> for WASM.</p>



<a name="184383460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184383460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184383460">(Dec 28 2019 at 17:28)</a>:</h4>
<p>I personally think that we should simply not expose inline asm for wasm.</p>



<a name="184383461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184383461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184383461">(Dec 28 2019 at 17:28)</a>:</h4>
<p>On most architectures, people really do rely on the fact that the exact instructions specified in the asm block are the ones that are executed. This is observable in various ways such as signal handlers, debugging through ptrace, and even just reading the instruction bytes from memory. None of these are available in WASM, which is why the compiler is allowed to mess around with instructions at the machine code level.</p>



<a name="184386260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184386260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184386260">(Dec 28 2019 at 18:43)</a>:</h4>
<p>We should expose it</p>



<a name="184386264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184386264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184386264">(Dec 28 2019 at 18:43)</a>:</h4>
<p>We should simply say that on wasm it's not assured to include those final instructions, if we have to do that</p>



<a name="184390024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184390024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184390024">(Dec 28 2019 at 20:35)</a>:</h4>
<p>What if we initially allowed inline wasm <em>but not</em> with the <code>volatile</code> effect?</p>



<a name="184390674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184390674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184390674">(Dec 28 2019 at 20:53)</a>:</h4>
<p>I'd rather just allow it with that caveat than mess with the <code>asm!</code> flags.</p>



<a name="184398879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184398879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184398879">(Dec 29 2019 at 01:16)</a>:</h4>
<p>Also works</p>



<a name="184652611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184652611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184652611">(Jan 02 2020 at 13:58)</a>:</h4>
<p>The only thing that worries me is that this is true for WASM today</p>



<a name="184652617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184652617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184652617">(Jan 02 2020 at 13:58)</a>:</h4>
<p>its unclear to me whether this will continue to be true for WASM in the future</p>



<a name="184652653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184652653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184652653">(Jan 02 2020 at 13:59)</a>:</h4>
<p>For example, consider <a href="https://github.com/WebAssembly/simd/issues/118" target="_blank" title="https://github.com/WebAssembly/simd/issues/118">https://github.com/WebAssembly/simd/issues/118</a></p>



<a name="184652730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184652730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184652730">(Jan 02 2020 at 14:00)</a>:</h4>
<p>For some final "targets" some WASM code generators can lower some vector shuffle sequences more efficiently than others</p>



<a name="184652761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184652761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184652761">(Jan 02 2020 at 14:00)</a>:</h4>
<p>that is, people actually want to specify different vector shuffles for WASM depending on which engine and which final target is that WASM going to end up running</p>



<a name="184652811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184652811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184652811">(Jan 02 2020 at 14:01)</a>:</h4>
<p>One can currently use inline assembly to tell LLVM to emit a particular sequence of instructions, e.g., that is known to be faster than the "optimized WASM" on say V8 targetting x86</p>



<a name="184652955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184652955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184652955">(Jan 02 2020 at 14:03)</a>:</h4>
<p>Arguably, this is a very hacky thing to do in the first place, but these hacks all break down if LLVM is able to optimize the WASM inline assembly, or if a generic WASM optimizer is used after the fact</p>



<a name="184683404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184683404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184683404">(Jan 02 2020 at 21:01)</a>:</h4>
<p>well nothing can <em>ever</em> assure that no one anywhere will use <code>wasm-opt</code> after the fact</p>



<a name="184683439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184683439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184683439">(Jan 02 2020 at 21:01)</a>:</h4>
<p>the same as you can't assure the programmer that no one will call <code>strip</code> or <code>objcopy</code> on their program after the fact</p>



<a name="184683545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184683545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184683545">(Jan 02 2020 at 21:02)</a>:</h4>
<p>so it comes down to how much info LLVM and other backend options will allow us to specify regarding "please don't do anything to alter this text",</p>



<a name="184683575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184683575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184683575">(Jan 02 2020 at 21:03)</a>:</h4>
<p>and if the final wasm is then further post-processed after rustc puts it on disk, that's user error</p>



<a name="184684336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184684336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184684336">(Jan 02 2020 at 21:11)</a>:</h4>
<p>Not necessarily user error. It's perfectly acceptable to run wasm-opt or similar tools.</p>



<a name="184684364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184684364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184684364">(Jan 02 2020 at 21:11)</a>:</h4>
<p>We should guarantee that <em>rustc</em> won't unexpectedly modify the assembly.</p>



<a name="184684433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184684433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184684433">(Jan 02 2020 at 21:12)</a>:</h4>
<p>I don't see that as incompatible with providing options that allow the user to invoke wasm-opt or similar, or with letting LLVM's backend run wasm-opt or equivalent, as long as there's a way to avoid that if you don't want it.</p>



<a name="184735335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/WebAssembly/near/184735335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/WebAssembly.html#184735335">(Jan 03 2020 at 13:59)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> <span class="user-mention" data-user-id="239881">@Josh Triplett</span> FWIW I was responding to <span class="user-mention" data-user-id="143274">@Amanieu</span> :</p>
<blockquote>
<p>I personally think that we should simply not expose inline asm for wasm.</p>
<p>On most architectures, people really do rely on the fact that the exact instructions specified in the asm block are the ones that are executed. This is observable in various ways such as signal handlers, debugging through ptrace, and even just reading the instruction bytes from memory. None of these are available in WASM, which is why the compiler is allowed to mess around with instructions at the machine code level.</p>
</blockquote>
<p>by providing an example where WASM users are already relying today on inline assembly not modifying their assembly strings, to workaround certain WASM "features", e.g., to implement certain WASM-engine and target specific optimizations in the WASM they generate against the goals of the "generic" WASM optimizer (LLVM) in our toolchain.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>